package com.gemme.gemme.wode;

import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.gemme.gemme.BaseActivity;
import com.gemme.gemme.R;
import com.gemme.gemme.login.Constant;
import com.gemme.gemme.manager.DataManager;
import com.gemme.gemme.manager.DataPares;
import com.gemme.gemme.test.MyPhotoActivity;
import com.gemme.gemme.test.MyPhotoFragment;
import com.gemme.gemme.util.SPUtil;
import com.gemme.gemme.view.CommentView;
import com.gemme.gemme.view.GMPhotoView;
import com.gemme.gemme.view.OnChangeLIstener;
import com.gemme.gemme.view.SgDDView;
import com.gemme.gemme.view.Topview;
import com.google.android.material.tabs.TabLayout;
import com.jaeger.library.StatusBarUtil;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.tools.PictureFileUtils;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

//侧滑删除地址
public class EvaluateActivity extends BaseActivity{

    private Bundle savedInstanceState;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.savedInstanceState = savedInstanceState;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_evaluate);
        initTop();
        initView();
//        initPhone();
        getIntentData();
    }
    long id;
    private void getIntentData() {
        try {

            id = getIntent().getIntExtra("data", 0);
            if(id > -1){
                getData();
            }
        }catch (Exception e){

        }
    }
    DingdanListBean.DataBean dataBean;
    private void getData() {
        String url = DataManager.BaseUrl_panghu + "mall/shoporderdetail/" + id;



        HashMap<String, String> headMap = new HashMap<>();
        headMap.put(DataManager.AuthorizationKey,DataManager.getToken(mContext));
        DataManager.toServer2(mContext, url, DataManager.GET_TYPE, "", headMap, new DataManager.GeResponse() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void onResponse(String response) {
                try {

                    JSONObject jsonObject = new JSONObject(response);

                    String data = jsonObject.getJSONObject("data").getString("wholeOrderVo");
                    JSONObject jbInfo = jsonObject.getJSONObject("data").getJSONObject("paddressResquersVo");

                    dataBean = JSON.parseObject(data, DingdanListBean.DataBean.class);
                    String addressee = jbInfo.getString("addressee");
                    String address = jbInfo.getString("address");
                    sgDDView.setdata(dataBean);
                    /*tv_name.setText(addressee + "");
                    tv_address.setText(address + "");

                    tv_order_id.setText(dataBean.getOrderId() + "");
                    tv_create_time.setText(dataBean.getCreateTime() + "");
                    tv_brand.setText(dataBean.getBrandName());*/

                    System.out.println("");
                }catch (Exception e){

                }

            }
        });
    }


  /*  private MyPhotoFragment fragment;
    private void initPhone() {
        // 在部分低端手机，调用单独拍照时内存不足时会导致activity被回收，所以不重复创建fragment
        if (savedInstanceState == null) {
            // 添加显示第一个fragment
            fragment = new MyPhotoFragment();
            getSupportFragmentManager().beginTransaction().add(R.id.tab_content, fragment,
                    PictureConfig.EXTRA_FC_TAG).show(fragment)
                    .commit();
        } else {
            fragment = (MyPhotoFragment) getSupportFragmentManager()
                    .findFragmentByTag(PictureConfig.EXTRA_FC_TAG);
        }
    }*/
    SgDDView sgDDView;
    CommentView commentView;
    GMPhotoView photoView;
    TextView tv_pingjia;
    View submit;
    int level;
    EditText editV;
    private void initView() {
        sgDDView = findViewById(R.id.sgDDView);
        commentView = findViewById(R.id.commentView);
        photoView = findViewById(R.id.photoView);
        photoView.setMaxSelectNum(5);
        photoView.setChooseMode(PictureMimeType.ofImage());
        tv_pingjia = findViewById(R.id.tv_pingjia);

        editV = findViewById(R.id.editV);

        commentView.setOnChangeLIstener(new OnChangeLIstener() {
            @Override
            public void onChangeLIstener() {
                tv_pingjia.setText(commentView.getLevelStr() + "");

                level = commentView.getLevel();
            }
        });


        submit = findViewById(R.id.submit);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toServer();
            }
        });
    }

    private void toServer() {
      try {
          if (dataBean == null){
              Toast.makeText(mContext, "评论上传失败", Toast.LENGTH_SHORT).show();
              return;
          }
          String url = DataManager.BaseUrl + "mall/shopproductcomment/saveComment?" +
                  "productId=" + dataBean.getProductId() + 0L
                  +"&orderId=" + dataBean.getOrderId()
                  +"&userId=" + SPUtil.getDataForString(mContext,Constant.UserId,"")
                  +"&commentLevel=" + level
                  +"&commentContent=" + editV.getText() + ""
                  +"&createBy=" + SPUtil.getDataForString(mContext,Constant.UserId,"")
                  ;
         /* EvaluteBean evaluteBean = new EvaluteBean();
          evaluteBean.setProductId(dataBean.getProductId() + 0L);
          evaluteBean.setOrderId(dataBean.getOrderId());
          evaluteBean.setUserId(SPUtil.getDataForString(mContext,Constant.UserId,""));
          evaluteBean.setCommentLevel(level);
          evaluteBean.setCommentContent(editV.getText() + "");
//          evaluteBean.setPictureUrl("");

          String jsonString = JSON.toJSONString(evaluteBean);*/


          HashMap<String, String> headMap = new HashMap<>();
          headMap.put(DataManager.AuthorizationKey,DataManager.getToken(mContext));
          DataManager.toServer2(mContext, url, DataManager.POST_TYPE, "", headMap, new DataManager.GeResponse() {
              @RequiresApi(api = Build.VERSION_CODES.O)
              @Override
              public void onResponse(String response) {
                  try {
                      System.out.println("");
                      boolean sucess = DataPares.isSucess(mContext, response);
                      if(sucess){
                          Toast.makeText(mContext, "发布成功", Toast.LENGTH_SHORT).show();

                      }else{
                          Toast.makeText(mContext, "发布失败", Toast.LENGTH_SHORT).show();
                      }
                      OrderManager.pinglunEndOrder(mContext, id, new OnChangeLIstener() {
                          @Override
                          public void onChangeLIstener() {
                              finish();
                          }
                      });



                  }catch (Exception e){

                  }

              }
          });
      }catch (Exception e){

      }
    }


    private void initTop() {
//        StatusBarUtil.setColor(this,getResources().getColor(R.color.white_color) );
        StatusBarUtil.setTransparent(this);
//        StatusBarUtil.setTranslucent(this,250);
        Topview topview = findViewById(R.id.topview);
        topview.setContentText("发表评价");
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case PictureConfig.APPLY_STORAGE_PERMISSIONS_CODE:
                // 存储权限
                for (int i = 0; i < grantResults.length; i++) {
                    if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                        PictureFileUtils.deleteCacheDirFile(mContext, PictureMimeType.ofImage());
                    } else {
                        Toast.makeText(mContext,
                                getString(R.string.picture_jurisdiction), Toast.LENGTH_SHORT).show();
                    }
                }
                break;
        }
    }



}